home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / manchest.lha / MANCHESTER / manchester / 2.2 / MVC-Wires.st < prev    next >
Text File  |  1993-07-24  |  6KB  |  269 lines

  1. "    NAME        MVC-Wires
  2.     AUTHOR        ikp@cs.man.ac.uk
  3.     FUNCTION an MVC tutorial e.g. 
  4.     ST-VERSIONS    2.2
  5.     PREREQUISITES     
  6.     CONFLICTS    
  7.     DISTRIBUTION      world
  8.     VERSION        1.1
  9.     DATE    22 Jan 1989
  10. SUMMARY    MVC-Wires
  11.     is the simple MVC example presented in Trevor's Tektronix
  12.    notes. The omissions and embelishments mentioned in the notes have
  13.    been implemented, plus a few extras such as a yellow-button menu
  14.    which modifies the action performed by the red button.(2.2).IKP
  15. "!
  16. OrderedCollection variableSubclass: #Wire
  17.     instanceVariableNames: ''
  18.     classVariableNames: ''
  19.     poolDictionaries: ''
  20.     category: 'MVC-Wires'!
  21.  
  22.  
  23. !Wire methodsFor: 'accessing'!
  24.  
  25. length
  26.     "answer the length of the wire"
  27.  
  28.     | total first next |
  29.     total _ 0.
  30.     self size > 0
  31.         ifTrue: 
  32.             [first _ self first.
  33.             self do: 
  34.                 [:next | 
  35.                 total _ total + (first dist: next).
  36.                 first _ next]].
  37.     ^total!
  38.  
  39. pinNear: aPoint 
  40.     "answer the index of the pin nearest aPoint"
  41.  
  42.     | bestDistance bestIndex pin |
  43.     bestIndex _ 1.
  44.     self size > 2
  45.         ifTrue: 
  46.             [bestDistance _ aPoint dist: (self at: 1).
  47.             1 to: self size do:
  48.                 [:index |
  49.                 (aPoint dist: (pin _ self at: index)) < bestDistance
  50.                     ifTrue: 
  51.                         [bestDistance _ aPoint dist: pin.
  52.                         bestIndex _ index]]].
  53.     ^bestIndex!
  54.  
  55. randomIndex
  56.     "return an index for a randomly chosen pin"
  57.  
  58.     | aRandom |
  59.     aRandom _ Random new.
  60.     ^(aRandom next * self size) asInteger + 1! !
  61.  
  62. !Wire methodsFor: 'manipulating'!
  63.  
  64. shorten
  65.     "Make random changes to routing order. Keep only those changes that 
  66.     shorten the length."
  67.  
  68.     | i j minLength |
  69.     self size > 2
  70.         ifTrue: 
  71.             [minLength _ self length.
  72.             i _ self randomIndex.
  73.             j _ self randomIndex.
  74.             self swap: i with: j.
  75.             self length < minLength
  76.                 ifTrue: 
  77.                     [minLength _ self length.
  78.                     self changed]
  79.                 ifFalse: [self swap: i with: j]]! !
  80.  
  81.  
  82. MouseMenuController subclass: #WireController
  83.     instanceVariableNames: 'modeMessage '
  84.     classVariableNames: 'WireYellowButtonMenu WireYellowButtonMessage '
  85.     poolDictionaries: ''
  86.     category: 'MVC-Wires'!
  87.  
  88.  
  89. !WireController methodsFor: 'initialize'!
  90.  
  91. initialize
  92.     "perform controller instance initialization, then setup yellow button menu"
  93.  
  94.     super initialize.
  95.     self initializeYellowButtonMenu.
  96.     self modeMessage: #addPin!
  97.  
  98. initializeYellowButtonMenu
  99.     "initialize yellow button activities from class defaults"
  100.  
  101.     self yellowButtonMenu: WireYellowButtonMenu
  102.         yellowButtonMessages: WireYellowButtonMessage! !
  103.  
  104. !WireController methodsFor: 'control defaults'!
  105.  
  106. controlActivity
  107.  
  108.     sensor noButtonPressed
  109.         ifTrue: [model shorten].
  110.     super controlActivity!
  111.  
  112. isControlActive
  113.     "control is active provided the cursor is in our view and we don't want the blue button menu"
  114.  
  115.     ^(view containsPoint: sensor cursorPoint) & sensor blueButtonPressed not!
  116.  
  117. isControlWanted
  118.     "control is wanted provided we don't want the blue button menu"
  119.     ^self sensor blueButtonPressed not and: [super isControlWanted]! !
  120.  
  121. !WireController methodsFor: 'menu messages'!
  122.  
  123. addPin
  124.     "add pin to wire"
  125.  
  126.     self model add: (view inverseDisplayTransform: sensor waitNoButton).
  127.     model changed!
  128.  
  129. movePin
  130.     "move an existing point in wire"
  131.  
  132.     | old new index |
  133.     model size > 0
  134.         ifTrue:
  135.             [index _ model pinNear: (old _ self view inverseDisplayTransform: sensor waitButton).
  136.             sensor cursorPoint: (view displayTransform: (model at: index)).
  137.             new _ view inverseDisplayTransform: sensor waitNoButton.
  138.             model at: index put: new.
  139.             model changed]!
  140.  
  141. redButtonActivity
  142.     "perform the appropriate red button activity"
  143.  
  144.     self view topView isCollapsed
  145.         ifFalse: [self perform: modeMessage]!
  146.  
  147. removePin
  148.     "remove pin from wire"
  149.  
  150.     self model size > 0
  151.         ifTrue: 
  152.             [sensor cursorPoint:
  153.                 (view displayTransform:
  154.                     (model at: (model pinNear: (view inverseDisplayTransform: sensor cursorPoint)))).
  155.             model removeAtIndex: (model pinNear: (view inverseDisplayTransform: sensor waitNoButton)).
  156.             model changed]!
  157.  
  158. selectAddPin
  159.     "select add mode"
  160.  
  161.     self view topView newLabel: 'add pin'.
  162.     self modeMessage: #addPin!
  163.  
  164. selectMovePin
  165.     "select move mode"
  166.  
  167.     self view topView newLabel: 'move pin'.
  168.     self modeMessage: #movePin!
  169.  
  170. selectRemovePin
  171.     "select remove mode"
  172.  
  173.     self view topView newLabel: 'remove pin'.
  174.     self modeMessage: #removePin! !
  175.  
  176. !WireController methodsFor: 'private'!
  177.  
  178. modeMessage: aSymbol
  179.     "set this controllers mode message to aSymbol"
  180.  
  181.     modeMessage _ aSymbol! !
  182. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  183.  
  184. WireController class
  185.     instanceVariableNames: ''!
  186.  
  187.  
  188. !WireController class methodsFor: 'class initialization'!
  189.  
  190. initialize
  191.     "WireController initialize."
  192.  
  193.     WireYellowButtonMenu _ PopUpMenu labels: 'add\move\remove' withCRs.
  194.     WireYellowButtonMessage _ #(selectAddPin selectMovePin selectRemovePin)! !
  195.  
  196. WireController initialize!
  197.  
  198. View subclass: #WireView
  199.     instanceVariableNames: 'pen '
  200.     classVariableNames: ''
  201.     poolDictionaries: ''
  202.     category: 'MVC-Wires'!
  203.  
  204.  
  205. !WireView methodsFor: 'initialize-release'!
  206.  
  207. initialize
  208.     "perform default view initialization, and initialize our very own pet pen"
  209.  
  210.     super initialize.
  211.     pen _ Pen new up.
  212.     pen combinationRule: Form over! !
  213.  
  214. !WireView methodsFor: 'updating'!
  215.  
  216. update: nilParameter
  217.     "my model has changed - redraw the view"
  218.  
  219.     self display! !
  220.  
  221. !WireView methodsFor: 'displaying'!
  222.  
  223. displayView
  224.     "display a wire as a single pixel line."
  225.  
  226.     self topView isCollapsed
  227.         ifFalse: 
  228.             [self clearInside.
  229.             model do: [:pin | pen goto: (self displayTransform: pin); down].
  230.             pen up]! !
  231.  
  232. !WireView methodsFor: 'controller access'!
  233.  
  234. defaultControllerClass
  235.     "what I'd like my controller to be, given the choice"
  236.  
  237.     ^WireController! !
  238. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  239.  
  240. WireView class
  241.     instanceVariableNames: ''!
  242.  
  243.  
  244. !WireView class methodsFor: 'instance creation'!
  245.  
  246. open
  247.     "build and return a WireView onto an empty wire"
  248.  
  249.     ^self openOn: Wire new!
  250.  
  251. openOn: aWire
  252.     "build and return a WireView on an existing wire"
  253.  
  254.     | topView aView |
  255.     topView _ StandardSystemView model: nil label: 'add pin' minimumSize: 100@100.
  256.     aView _ self new borderWidth: 1.
  257.     aView insideColor: Form white.
  258.     aView model: aWire.
  259.     topView addSubView: aView.
  260.     topView controller open! !
  261.  
  262. !WireView class methodsFor: 'example'!
  263.  
  264. example
  265.     "accept a wire from the user and a viewport to display it in."
  266.     "WireView example"
  267.  
  268.      WireView openOn: Wire new! !
  269.